﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunboxsoft.WinForm;
using Sunboxsoft.ASMS.Client.UI.Properties;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using Sunboxsoft.Common;
namespace Sunboxsoft.ASMS.Client.UI.Edit
{
    /// <summary>
    /// 修改库房信息
    /// </summary>
    public partial class UpdateWareHouseView : UserControl, IView
    {
        Promise _promise = new Promise();
        WebRef.VWarehouse _vWarehouse = null;
        public UpdateWareHouseView()
        {
            InitializeComponent();
            ValidationManager.Register(this, new Common.Validations.TextEditIsNotNullValidation(this.ctlEditName, "仓库名称", false));
            ValidationManager.Register(this, new Common.Validations.TextEditIsNotNullValidation(this.ctlEditAddress, "详细地址", true));
            ValidationManager.Register(this, new Common.Validations.TextEditIsNotNullValidation(this.ctlEditContactName, "联系人姓名", true));
            ValidationManager.Register(this, new Common.Validations.TextEditTelephoneValidation(this.ctlEditTel, true));
            ValidationManager.Register(this, new Common.Validations.TextEditIsNotNullValidation(this.ctlEditDes, "描述", true));
        }
        public UpdateWareHouseView(WebRef.VWarehouse vWarehouse)
            : this()
        {
            this._vWarehouse = vWarehouse;
        }

        #region IView 成员

        public void RegisterAction(ActionNodeCollection collection)
        {
            collection.AddAction(new ActionNode("save", Resources.isOK, "保存", btnSave_Click))
                .AddAction(new ActionNode("cancel", Resources.isBack, "取消", btnCancel_Click));
        }

        public void Active(ViewActiveArgs args)
        {
            _promise.Then(() =>
            {
                SearchLookUpEditUtil.BindEMasterDataItem(this.ctlLookUpCountry,
                    _promise,
                    Domain.Constant.MasterDataCodes.Country,
                    null, null, true, true);
            })
            .Then(() =>
            {
                this.BeginInvoke(new Action(() =>
                {
                    GetVWareHouse();
                    _promise.Resolve();
                }));
            })
            .Run();
        }

        public void Resume(ViewResumeArgs args)
        {
        }

        public ViewHeader GetHeader()
        {
            return new ViewHeader("修改库房信息");
        }
        #endregion

        #region Mehtod
        private void GetVWareHouse()
        {
            if (_vWarehouse != null)
            {
                this.ctlEditCode.Text = _vWarehouse.Code;
                this.ctlEditName.Text = _vWarehouse.Name;
                this.ctlEditAddress.Text = _vWarehouse.Address;
                this.ctlEditContactName.Text = _vWarehouse.ContactName;
                this.ctlEditTel.Text = _vWarehouse.ContactTelephone;
                this.ctlEditDes.Text = _vWarehouse.Description;
            }
        }
        /// <summary>
        /// 修改库房
        /// </summary>
        public void UpdateWareHouse()
        {
            WebRef.EWarehouse model = new WebRef.EWarehouse();
            model.Name = this.ctlEditName.Text.Trim();
            model.Country = SearchLookUpEditUtil.GetSelectedValue<WebRef.EMasterDataItem>(this.ctlLookUpCountry).GetCode();
            model.Province = SearchLookUpEditUtil.GetSelectedValue<WebRef.EMasterDataItem>(this.ctlLookUpProvince).GetCode();
            model.City = SearchLookUpEditUtil.GetSelectedValue<WebRef.EMasterDataItem>(this.ctlLookUpCity).GetCode();
            model.District = SearchLookUpEditUtil.GetSelectedValue<WebRef.EMasterDataItem>(this.ctlLookUpDistrict).GetCode();
            model.Address = this.ctlEditAddress.Text.Trim();
            model.ContactName = this.ctlEditContactName.Text.Trim();
            model.ContactTelephone = this.ctlEditTel.Text.Trim();
            model.Description = this.ctlEditDes.Text.Trim();

            this.ShowMask("loading...");
            UIContext.Current.InvokeAsync(() =>
            {
                ServiceResult result = new Service.UpdateService().UpdateWarehouse(_vWarehouse.Id, model);
                this.BeginInvoke(new Action(() =>
                {
                    this.HideMask();
                    if (result.IsSuccess)
                    {
                        UIContext.Current.ViewManager.CloseView(true, null);
                        _promise.Resolve();
                    }
                    else
                    {
                        UIContext.Current.ViewManager.ShowError(result.Message);
                        _promise.Reject();
                    }
                }));
            });
        }
        #endregion

        #region Action
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, object e)
        {
            string strMess = null;
            if (ValidationManager.Validate(this, out strMess))
            {
                UpdateWareHouse();
            }
            else
            {
                UIContext.Current.ViewManager.ShowWarning(strMess);
            }
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, object e)
        {
            UIContext.Current.ViewManager.CloseView(false, null);
        }
        /// <summary>
        /// 根据选择国家获取省份
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lupCountry_EditValueChanged(object sender, EventArgs e)
        {
            Promise promise = new Promise();
            promise.Then(() =>
            {
                SearchLookUpEditUtil.BindEMasterDataItem(this.ctlLookUpProvince, promise,
                   Domain.Constant.MasterDataCodes.Province,
                   SearchLookUpEditUtil.GetSelectedValue<WebRef.EMasterDataItem>(this.ctlLookUpCountry).GetCode(),
                   null,
                   false, true);
            }).Then(() =>
            {
                this.BeginInvoke(new Action(() =>
                {
                    if (_vWarehouse != null)
                    {
                        ctlLookUpProvince.SelectEMasterDataItem(_vWarehouse.Province);
                        promise.Resolve();
                    }
                }));
            }).Run();
        }
        /// <summary>
        /// 根据选择省获取市
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lupProvince_EditValueChanged(object sender, EventArgs e)
        {
            Promise promise = new Promise();
            promise.Then(() =>
            {
                SearchLookUpEditUtil.BindEMasterDataItem(this.ctlLookUpCity, promise,
                    Domain.Constant.MasterDataCodes.City,
                     SearchLookUpEditUtil.GetSelectedValue<WebRef.EMasterDataItem>(this.ctlLookUpProvince).GetCode(),
                     null, false, true);
            }).Then(() =>
            {
                this.BeginInvoke(new Action(() =>
                {
                    if (_vWarehouse != null)
                    {
                        ctlLookUpCity.SelectEMasterDataItem(_vWarehouse.City);
                        promise.Resolve();
                    }
                }));
            }).Run();
        }
        /// <summary>
        /// 根据市获取区
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lupCity_EditValueChanged(object sender, EventArgs e)
        {
            if (this.ctlLookUpCity.EditValue == null)
                return;
            Promise promise = new Promise();
            promise.Then(() =>
            {
                SearchLookUpEditUtil.BindEMasterDataItem(this.ctlLookUpDistrict, promise,
                    Domain.Constant.MasterDataCodes.District,
                    SearchLookUpEditUtil.GetSelectedValue<WebRef.EMasterDataItem>(this.ctlLookUpCity).GetCode(), null, false, true);
            }).Then(() =>
            {
                this.BeginInvoke(new Action(() =>
                {
                    if (_vWarehouse != null)
                    {
                        ctlLookUpDistrict.SelectEMasterDataItem(_vWarehouse.District);
                        promise.Resolve();
                    }
                }));
            }).Run();
        }
        #endregion
    }
}
